package ext.tianma.masschange.datautility;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;

import wt.part.QuantityUnit;
import wt.part.WTPart;
import wt.part.WTPartUsageLink;
import wt.session.SessionHelper;
import wt.util.WTException;
import wt.util.WTProperties;

import com.ptc.core.components.descriptor.ModelContext;
import com.ptc.core.components.factory.AbstractDataUtility;
import com.ptc.core.components.rendering.guicomponents.ComboBox;

import ext.tianma.masschange.util.MassChangeUtil;

public class MassChangeUnitDataUtility extends AbstractDataUtility {
	
	private static String RESOURCE = "ext.tianma.masschange.resource.MassChangeResource";
	private static String CLASSNAME="";
	private static boolean VERBOSE = false;
	private static WTProperties wtProperties;
	private static Locale locale = null;
	private static QuantityUnit quantityUnitSet[] = null;

	static {

		try {
			CLASSNAME= MassChangeUnitDataUtility.class.getName();
			wtProperties = WTProperties.getLocalProperties();
			locale = SessionHelper.manager.getLocale();
			VERBOSE = wtProperties.getProperty("ext.tianma.masschange.datautility.verbose", false);
			quantityUnitSet = QuantityUnit.getQuantityUnitSet();
		}
		catch (IOException ioe) {
			ioe.printStackTrace();
		}catch (Throwable throwable) {
			throw new ExceptionInInitializerError(throwable);
		}
	}
	public MassChangeUnitDataUtility() {

	}
	  /**
	 *输出调试信息
	 */
	private static void outDebugInfo(String info) {
		if(VERBOSE)
			System.out.println(CLASSNAME+"......"+info);
	}	

	@Override
	public Object getDataValue(String component_id, Object obj, ModelContext mc) throws WTException {
		ArrayList inernalValues = new ArrayList();
		ArrayList displayValues = new ArrayList();
		ArrayList selectedValue = new ArrayList();
		for(int i=0; i<quantityUnitSet.length; i++) {
			QuantityUnit quantityUnit1 = quantityUnitSet[i];
			inernalValues.add(quantityUnit1.getStringValue());
			displayValues.add(quantityUnit1.getDisplay(locale));
		}
		ComboBox combo = new ComboBox();
		
		combo.setInternalValues(inernalValues);
		combo.setValues(displayValues);
		String unit = "每个";
		WTPart part = null;
		if(obj instanceof WTPartUsageLink) {
			WTPartUsageLink link = (WTPartUsageLink)obj;
			unit = link.getQuantity().getUnit().getDisplay(locale);	
			part = link.getUsedBy();
		} else if(obj instanceof WTPart) {
			part = (WTPart)obj;
		}
		selectedValue.add(unit);
		combo.setSelected(selectedValue);
		//判断是否在CA中或者在审阅中或者已经废弃
		if(MassChangeUtil.hasCA(part, mc)||MassChangeUtil.equalState(part, "UNDERREVIEW")||MassChangeUtil.equalState(part, "OBSOLESCENCE")) {
			return unit;			
		}
		return combo;
	}
}
